/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.message;

import javax.jms.JMSException;

import com.sinodata.bsm.common.message.AsyncSession;
import com.sinodata.bsm.common.message.ServiceMessage;
import com.sinodata.bsm.common.message.SyncSession;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-14 下午4:09:53          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public final class Messenger {
    private static Messenger instance = new Messenger();

    /**
     * 
     */
    private Messenger() {
    }

    public static Messenger getInstance() {
        return instance;
    }

    /**
     * 发送同步消息
     * @param probeId
     * @param msg
     * @return
     * @throws JMSException
     */
    public Object sendSyncMessage(Long probeId, ServiceMessage msg) throws JMSException {
        String queueName = "SYNC_CTP_" + probeId;
        SyncSession session = null;
        try {
            session = new SyncSession(queueName);
            msg.setNeedReply(true);
            return session.send(msg);
        } catch (JMSException e) {
            throw e;
        } finally {
            session.close();
        }
    }

    /**
     * 发送异步消息
     * @param probeId
     * @param msg
     * @throws JMSException
     */
    public void sendAsynMessage(Long probeId, ServiceMessage msg) throws JMSException {
        String queueName = "ASYN_CTP_" + probeId;
        AsyncSession session = null;
        try {
            session = new AsyncSession(queueName);
            msg.setNeedReply(false);
            session.send(msg);
        } catch (JMSException e) {
            throw e;
        } finally {
            session.close();
        }

    }
}
